package api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"seckill/service"
	"seckill/utils"
	"strconv"
)

func SetStore(ctx *gin.Context) {
	service.SetStore(1, 5)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    1,
		"message": "库存设置成功",
	})
}

func SeckillGoods(ctx *gin.Context) {
	for i := 0; i < 200; i++ {
		utils.Wg.Add(1)
		go service.QueryRedisV1(1, i)
	}
	utils.Wg.Wait()
	ctx.JSON(http.StatusOK, gin.H{
		"code":    1,
		"message": "抢购完成",
	})
}

func SeckillGoods2(ctx *gin.Context) {
	userId := ctx.DefaultQuery("userId", "")
	userIdInt, _ := strconv.Atoi(userId)
	v2, err := service.QueryRedisV2(1, userIdInt)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    1,
			"message": err.Error(),
		})
		return
	}
	fmt.Println(v2, "------------------")
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": v2,
	})
	return
}
